package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
@Service
@Transactional
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper mapper;
    @Override
    public void add(ProductBean productBean) {
        mapper.insert(productBean);
    }

    @Override
    public void del(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public void update(Integer id, Integer price) {
        ProductBean productBean = new ProductBean();
        productBean.setId(id);
        productBean.setPrice(price);
        mapper.updateById(productBean);
    }

    @Override
    public List<ProductBean> findAll() {

        return mapper.selectList(null);
    }

    @Override
    public ProductBean findById(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public List<ProductBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (name!=null && name.length()!=0){
            qw.like("p_name",name);
        }
        if (startDate!=null){
            qw.ge("p_createDate",startDate);
        }
        if (endDate!=null){
            qw.le("p_createDate",endDate);
        }
        return mapper.selectList(qw);

    }
    /**
     * 动态模糊分页查询
     * @param pageNo 页码
     * @param name 商品名
     * @param startDate 起始日期
     * @param endDate 结束日期
     * @return 返回商品集合对象
     */
    public IPage<ProductBean> findByItems (Integer pageNo, String name, LocalDate startDate, LocalDate endDate){
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        if (name!=null&&name.length()!=0){
            qw.like("p_name",name);
        }
        if (startDate!=null){
            qw.ge("p_createDate",startDate);
        }
        if (endDate!=null){
            qw.le("p_createDate",endDate);
        }
        return mapper.selectPage(new Page(pageNo,3),qw);
    }
}
